package com.mezz.bones.framework.core.aspect;

import com.mezz.bones.framework.core.annotation.DegradeNull;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @Author: zhuang
 * @Date: 2024/4/9 16:48
 * @Description: TODO
 * @Version: 1.0
 **/

@Aspect
@Component
@Slf4j
public class DegradeAspect {

    // 定义一个切入点，匹配你想要应用此切面的所有方法
    @Pointcut("@annotation(degradeNull))")
    public void myMethods(DegradeNull degradeNull) {
    }

    // 使用@Around通知环绕目标方法的执行
    @Around(value = "@annotation(degradeNull)")
    public Object handleMethodExecution(ProceedingJoinPoint joinPoint, DegradeNull degradeNull) throws Throwable {
        try {
            // 调用目标方法
            return joinPoint.proceed();
        } catch (Throwable e) {
            // 如果目标方法抛出异常，返回null
            log.error("Method:{}, threw an exception",joinPoint.getSignature(),e);
            return null;
        }
    }
}
